
import { app, BrowserWindow, Menu, globalShortcut, DesktopCapturerSource } from 'electron'
// const { resolve, dirname } = require("path");
import path from 'path'
// const menu = Menu.buildFromTemplate([]);
// Menu.setApplicationMenu(menu);


//获取单例锁
const gotTheLock = app.requestSingleInstanceLock();
if (!gotTheLock) {
  app.quit();
} else {
  let win: BrowserWindow | null;
  async function createWindow() {
    const win = new BrowserWindow({
      width: 1700,
      height: 1000,
      // resizable: false,
      //useContentSize: true,
      /* transparent: true, */
      icon: path.join(__dirname, "view/favicon.ico"),
      // frame: false,
      show: true,

      webPreferences: {
        // enableRemoteModule: false,
        webSecurity: false,
        nodeIntegration: true,
        contextIsolation: true,
        webviewTag: true,
        preload: path.join(__dirname, "preload.js"),
      },
    });

    win.webContents.on('did-finish-load', async () => {
      if (win) {
        setTimeout(() => {
        
          console.log();
          // desktopCapturer.getSources({ types: ['window', 'screen'] }).then(async sources => {
          //   win.webContents.send('SEND_SCREEN_SHARE_SOURCES', sources);

          // });
        }, 3000);
      }
    });
    await win.loadFile(path.join(__dirname, '../', "view/index.html"));
    // await win.loadFile(path.join(__dirname, '../', "view/getDisplayMedia.html"));

    return win;
  }
  app.on("ready", async function () {
    win = await createWindow();

    globalShortcut.register("Alt+B", function () {
      win!.webContents.openDevTools();
    });
    win.webContents.openDevTools();


    win.on("closed", () => {
      win = null;
    });
    win.on("ready-to-show", () => {
      win!.show();
    });
  });


  app.on("quit", () => {
    app.releaseSingleInstanceLock(); //释放所有的单例锁
  });
}
